<template>
  <div>
    <div class="iconBox" v-on:click="isShow">
      <van-icon :size="size" :color="color" v-if="type==='zd'" name="arrow-down"/>
      <van-icon :size="size" :color="color" v-else name="arrow"/>
    </div>
  </div>
</template>

<script>
import Vue from 'vue';

export default {
  props: ['value', "initData", "abInitFirstRow", "index", 'elementid', 'initShow'],
  data: function () {
    return {
      // isShow: true
      type: 'zd',
      size: '14',
      color: '#C8C8CD',
    }
  },
  mounted: function () {
    // if(this.abInitFirstRow){
    //   //如果有编辑权限才会自动添加行
    //   let elPermission = this.getPermission();
    //   if(!elPermission || elPermission === "n" || elPermission === "r"){
    //     return;
    //   }
    //
    //   if(!this.value || this.value.length ==0){
    //     this.addSub();
    //   }
    // }else{
    //   if(!this.value){
    //     this.$emit('input', []);
    //   }
    // }
  },
  created() {
    this.$nextTick(function () {
      let Div = document.getElementById(this.elementid)
      let aDiv = Div.getElementsByClassName("weui-cells_form")[0]
      //当前是展开还是关闭状态
      if (this.initShow) {
        aDiv.style.display = "block"
        this.type = 'zd'
      } else {
        aDiv.style.display = 'none'
        this.type = 'zk'
      }
    })
  },
  methods: {
    isShow() {
      let Div = document.getElementById(this.elementid)
      let aDiv = Div.getElementsByClassName("weui-cells_form")[0]
      if (aDiv.style.display == 'none') {
        aDiv.style.display = "block"
        this.type = 'zd'
      } else {
        aDiv.style.display = 'none'
        this.type = 'zk'
      }
    },
    addSub: function () {
      let list = this.value;
      if (!list) {
        list = [];
        this.$emit('input', list);
      }
      var json = JSON.parse(JSON.stringify(this.initData));//深copy
      //获取子表中的最大$$abHashCode
      var max = 0;
      list.forEach(function (item) {
        if (item["$$abHashCode"] > max) {
          max = item["$$abHashCode"];
        }
      });
      json["$$abHashCode"] = max + 1;
      if (this.index >= 0) {//在指定下标后插入
        list.splice((this.index + 1), 0, json);
      } else {//插入底部
        list.push(json);
      }
    },
    // 获取权限标签的值
    getPermission: function () {
      for (let i = 0, d; d = this.$vnode.data.directives[i++];) {
        if (d.name == "ab-permission") {
          return d.value;
        }
      }
      return null;
    }
  }
}
</script>
<style lang="less" scoped>
.iconBox {
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
}
</style>
